package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;

/*    *//**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     *//*
    @Override
    public NursingPlan selectNursingPlanById(Integer id)
    {
        return getById(id);
    }*/
@Autowired
private INursingProjectPlanService nursingProjectPlanService;
    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //1.查询护理计划
        NursingPlan nursingPlan = this.getById(id);
        //2.查询中间表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //3.数据封装
        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(projectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).toList();
        vo.setProjectPlans(nursingProjectPlanVos);

        return vo;
    }
    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        //1.新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        this.save(nursingPlan);
        //2.新增护理计划项目
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
        });
        boolean flag = nursingProjectPlanService.saveBatch(projectPlans);
        return flag ? 1 : 0;
    }

    /*    */
    /**
     * 新增护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    /*
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {

        //return nursingPlanMapper.insertNursingPlan(nursingPlan);
        return save(nursingPlan) ? 1 : 0;
    }*/

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        //1.修改中间表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if(CollUtil.isNotEmpty(projectPlans)){
            //删除
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            //新增
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        //2.修改计划表
        boolean flag = this.updateById(nursingPlan);
        return flag ? 1 : 0;
    }

/*    *//**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     *//*
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }*/

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        //qy 如果此条计划是启用状态，不能删除
        //qy 如果此条计划被绑定了，也不能删除
        //1.删除中间表
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //2.删除计划表
        boolean flag = this.removeById(id);
        return flag == true ? 1 : 0;
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> getNursingPlanAll() {
        List<NursingPlan> list = this.list();
        return list;
    }

}
